package com.hsmpay.mobile.action.other;

import com.hsmpay.mobile.action.base.MobileBaseAction;
import com.hsmpay.mobile.pojo.other.Help;
import com.hsmpay.mobile.pojo.other.HelpDetail;
import com.hsmpay.mobile.service.other.HelpDetailService;
import com.hsmpay.mobile.service.other.HelpService;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.*;

/**
 * 用户反馈 action
 *
 * @author Steven
 */
@Controller("helpAction")
@Scope("prototype")
public class HelpAction extends MobileBaseAction {
    private static final long serialVersionUID = -337060378804571678L;
    private static Logger log = Logger.getLogger(HelpAction.class);
    //--- 帮助 ------------------
    @Resource(name = "helpService")
    private HelpService<Help, Long> helpService;
    //--- 帮助详情 ---------------
    private HelpDetail helpDetail;
    private List<HelpDetail> helpDetailList;
    @Resource(name = "helpDetailService")
    private HelpDetailService<HelpDetail, Long> helpDetailService;
    //--- 参数 -------------
    private Integer version;

    /**
     * 用户帮助
     *
     * @return
     */
    public void searchHelpList() throws Exception {
        log.debug("--- searchHelpList() start ---");
        JSONObject js = JSONObject.fromObject(getRequest().getAttribute(FC_DATA));

        roleId = Long.parseLong(js.getString("roleId"));
        terminalTypeId = Long.parseLong(js.getString("terminalTypeId"));
        if (js.containsKey("version"))
            version = Integer.parseInt(js.getString("version"));
        log.debug("version = " + version);
        try {
            if (null == roleId) {
                log.warn("进入用户帮助时, 传入参数roleId为空");
                sendAppErrorMsg("6001", "roleId");
                return;
            }
            if (null == version) {
                log.warn("进入用户帮助时, 传入参数version为空");
                sendAppErrorMsg("6001", "version");
                return;
            }
            Map<String, Object> result = new HashMap<String, Object>();
            List<Map<String, Object>> helpList = new ArrayList<Map<String, Object>>();
            //判断帮助是否为最新版本
            int maxVersion = helpService.getMaxVersion(terminalTypeId, roleId);
            if (maxVersion == version) {
                log.debug("版本尚未更新;");
                result.put("reqCode", "0000");
                result.put("reqMsg", "版本尚未更新！");
                result.put("helpList", helpList);
                sendAppMsg(result);
                return;
            }
            helpDetail = new HelpDetail();
            Help helpObj = new Help();
            helpObj.setTerminalTypeId(terminalTypeId);
            helpObj.setRoleId(roleId);
            helpObj.setVersion(maxVersion);
            helpDetail.setHelp(helpObj);
            helpDetailList = helpDetailService.searchEntityList(helpDetail);
            Map<String, Object> help = new LinkedHashMap<String, Object>();
            Map<String, Object> element = new LinkedHashMap<String, Object>();
            for (HelpDetail hd : helpDetailList) {
                element.put("title", hd.getTitle());
                element.put("text", hd.getText());
                helpList.add(element);
                element = new LinkedHashMap<String, Object>();
            }
            help.put("version", helpDetail.getHelp().getVersion());
            help.put("helpList", helpList);

            help.put("reqCode", "0000");
            help.put("reqMsg", "获得用户帮助列表成功！");
            sendAppMsg(help);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            log.debug("--- searchHelpList() end ---");
        }
    }

    //---------------------set get start--------------------
    public HelpDetail getHelpDetail() {
        return helpDetail;
    }

    public void setHelpDetail(HelpDetail helpDetail) {
        this.helpDetail = helpDetail;
    }

    public List<HelpDetail> getHelpDetailList() {
        return helpDetailList;
    }

    public void setHelpDetailList(List<HelpDetail> helpDetailList) {
        this.helpDetailList = helpDetailList;
    }

    public Integer getVersion() {
        return version;
    }

    public void setVersion(Integer version) {
        this.version = version;
    }
    //---------------------set get end----------------------

}
